<template>
  <div class="page-container">
    <div class="common-content-wrap">
      <TrTable
        ref="trTable"
        isNeedSelection
        :btnList="btnList"
        :total="totalNum"
        v-model="searchParams"
        :searchShowList="searchShowList"
        :searchHideList="searchHideList"
        :minSearchLabelWidth="100"
        :tableColumn="tableColumn"
        requestName="searchHandle"
        :tableData="tableData"
        @import="operateHandle($event, 'import')"
        @export="operateHandle($event, 'export')"
        @remove="operateHandle($event, 'remove')"
      />
      <TrUploadFile
        v-model="isShowUpload"
        :limitNum="1"
        typeTips="支持.xlsx,.xls格式，最多添加1个附件"
        templateName="收货计划导入模板.xlsx"
        templateType="ReceiptPlanUploadVO"
        @confrim="confrimUpload"
      />
    </div>
  </div>
</template>

<script>
import {
  getList,
  remove,
  importTableData,
} from "@/api/logistics/receivingManagement";
import { comExport } from "@/utils/util.js";
import TrTable from "@/components/Tr-table/Tr-table.vue";
import TrUploadFile from "@/components/Tr-upload/Tr-upload-file.vue";

export default {
  components: {
    TrTable,
    TrUploadFile,
  },
  data: () => ({
    tableColumn: [
      {
        type: "text",
        label: "采购订单编号",
        value: "purchaseOrderCode",
      },
      {
        type: "text",
        label: "供应商编码",
        value: "supplierCode",
      },
      {
        type: "text",
        label: "供应商名称",
        value: "supplierName",
      },
      {
        type: "text",
        label: "物料标签",
        value: "materialBarcode",
      },
      {
        type: "text",
        label: "炉号",
        value: "furnaceCode",
      },
      {
        type: "text",
        label: "卷号",
        value: "volumeCode",
      },
      {
        type: "text",
        label: "原料料号",
        value: "materialCode",
      },
      {
        type: "text",
        label: "原料箱号",
        value: "boxCode",
      },
      {
        type: "text",
        label: "物料名称",
        value: "materialName",
      },
      {
        type: "text",
        label: "物料规格",
        value: "materialSpec",
      },
      {
        type: "text",
        label: "数量",
        value: "num",
      },
      {
        type: "text",
        label: "数量单位",
        value: "newNumUnit",
      },
      {
        type: "text",
        label: "重量",
        value: "weight",
      },
      {
        type: "text",
        label: "重量单位",
        value: "newWeightUnit",
      },
      {
        type: "text",
        label: "创建人员",
        value: "createUserName",
      },
      {
        type: "text",
        label: "创建时间",
        value: "createTime",
        width: 160,
      },
    ],
    tableData: [],
    searchHideList: [
      {
        id: "purchaseOrderCode",
        type: "input",
        width: 33.33,
        label: "采购订单编号",
        value: "",
      },
      {
        id: "volumeCode",
        type: "input",
        width: 33.33,
        label: "卷号",
        value: "",
      },
      {
        id: "supplierCodeId",
        type: "loadselect",
        width: 33.33,
        label: "供应商编码",
        value: "",
        list: [],
        requestObj: {
          url: "/api/mes-warehouse-service/supplier/page",
          method: "post",
          searchKey: "supplierCode",
          rlLabel: "supplierCode",
          rlValue: "id",
          resultKey: "data,data,records",
        },
      },
      {
        id: "supplierNameId",
        type: "loadselect",
        width: 33.33,
        label: "供应商名称",
        value: "",
        list: [],
        requestObj: {
          url: "/api/mes-warehouse-service/supplier/page",
          method: "post",
          searchKey: "supplierName",
          rlLabel: "supplierName",
          rlValue: "id",
          resultKey: "data,data,records",
        },
      },
      {
        id: "materialBarcode",
        type: "input",
        width: 33.33,
        label: "物料标签",
        value: "",
      },
      {
        id: "materialCodeId",
        type: "loadselect",
        width: 33.33,
        label: "原料料号",
        value: "",
        list: [],
        requestObj: {
          url: "/api/mes-production-service/material/page",
          method: "post",
          searchKey: "materialCode",
          rlLabel: "materialCode",
          rlValue: "id",
          resultKey: "data,data,records",
        },
      },
      {
        id: "materialNameId",
        type: "loadselect",
        width: 33.33,
        label: "物料名称",
        value: "",
        list: [],
        requestObj: {
          url: "/api/mes-production-service/material/page",
          method: "post",
          searchKey: "materialName",
          rlLabel: "materialName",
          rlValue: "id",
          resultKey: "data,data,records",
        },
      },
      {
        id: "createUser",
        type: "loadselect",
        width: 33.33,
        label: "创建人员",
        value: "",
        list: [],
        requestObj: {
          url: "/api/trio-user/page",
          method: "get",
          searchKey: "realName",
          rlLabel: "realName",
          rlValue: "account",
          resultKey: "data,data,records",
        },
      },
    ],
    searchShowList: [
      {
        isNeed: true,
        startStr: "createTimeStart",
        endStr: "createTimeEnd",
        id: "createdTime",
        type: "daterange",
        width: 80,
        label: "创建时间",
        value: [],
        radio: 1,
        timePeriod: true,
      },
    ],
    btnList: [
      {
        label: "导入",
        option: "import",
        color: "blue",
      },
      {
        label: "导出",
        option: "export",
        color: "white",
      },
      {
        label: "删除",
        option: "remove",
        color: "white",
        isRelaTable: true,
      },
    ],
    searchParams: {
      current: 1,
      size: 10,
    },
    totalNum: 0,
    isShowUpload: false,
  }),
  methods: {
    operateHandle(val, type) {
      console.log(val, type);
      this.$validateData(val, 3);
      switch (type) {
        case "import":
          this.isShowUpload = true;
          break;
        case "export":
          this.$exportHandle({ vm: this, params: this.searchParams });
          break;
        case "remove":
          this.remove(this.ids);
          break;
      }
    },
    closeDetailPopup() {
      this.isShowDetailPopup = false;
    },
    confrimHandle() {
      if (this.currentHandle == "rowEdit") {
        this.formObj.id = this.editId;
        this.edit(this.formObj);
      } else if (this.currentHandle == "add") {
        this.add(this.formObj);
      }
      this.isShowEdit = false;
    },
    searchHandle() {
      this.getListHandle();
    },
    async confrimUpload(val) {
      const { status, data, statusText, headers } = await importTableData({
        file: val[0],
      });
      let falg =
        toString.call(data) === "[object Blob]" &&
        headers["content-type"]?.includes("application/");
      if (status != 200) return this.$message.error(statusText);
      if (falg) {
        this.$message.error("导入失败，请查看结果");
        let fileName = headers["content-disposition"].split("filename=")[1];
        comExport(data, decodeURI(fileName));
      } else {
        this.$message.success(this.$t("msg.importSuccess"));
      }
      this.isShowUpload = false;
      this.getListHandle();
    },
    remove(idList) {
      this.$textShow({
        title: this.$t("tip.label"),
        content: [this.$t("tip.delete")],
      })
        .then(async () => {
          let res = await remove({ idList });
          if (res.data.code != 200) return;
          this.$message.success(this.$t("msg.success"));
          this.$refs.trTable.clearSelect();
          this.getListHandle();
        })
        .catch(() => {});
    },
    getListHandle() {
      getList(this.searchParams).then((res) => {
        if (res.data.code != 200) return;
        let data = res.data.data;
        this.$validateData(data);
        this.tableData = data.records;
        this.tableData.forEach((item) => {
          item.newNumUnit = `${item.numUnitCode}(${item.numUnitName})`;
          item.newWeightUnit = `${item.weightUnitCode}(${item.weightUnitName})`;
        });
        this.totalNum = data.total;
      });
    },
  },
  mounted() {
    // this.getListHandle();
  },
  computed: {
    ids() {
      let ids = [];
      this.$refs.trTable.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    },
  },
};
</script>

<style></style>
